﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Agro.Library;
using Ninject;
using Agro.Services;
using Agro.Models;
using SN.Core;
using SN.Data;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Agro.Web.Areas.SystemManage.Controllers
{
    public class SubmitInfoController : BaseController
    {

        [Inject]
        public IReportService ReportService { get; set; }
        // GET: /SystemManage/SubmitInfo/
        public ActionResult ReportPublish()
        {
            return View();
        }

        /// <summary>
        /// 上报情况查询
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllReportState()
        {
            ViewBag.CurrOrganizationID = CurrUser.UserInfo.User_OrganizationID;
            return View();
        }

        public ActionResult GetReportNameList()
        {
            DataTable dataTable = ReportService.GetReportNameList(CurrUser);
            
            string result = JsonConvert.SerializeObject(dataTable, new DataTableConverter());
            return Content(result);
        }

        [HttpPost]
        public ActionResult GetReportStateList(string reportid, QueryBase query)
        {

            if (string.IsNullOrEmpty(reportid)) return Json("");
            string wclip = "Organization_ID in (select Organization_ID from T_Organization where Organization_ParentId="+CurrUser.UserInfo.User_OrganizationID.ToString()+")";
            string wSubmitinfoclip = "SubmitInfo_OrganizationID = Organization_ID and SubmitInfo_ReportID='" + reportid + "'";
                
            string querydata = Request["queryData"];
            Dictionary<string, string> dic = WebUtility.ConvertQueryToDictionary(querydata);
            foreach (KeyValuePair<string, string> item in dic)
            {
                switch (item.Key)
                {
                    case "ListYear":
                        wSubmitinfoclip +=" and SubmitInfo_Year=" + item.Value.ToString();
                        break;
                    case "ListMonth":
                        wSubmitinfoclip +=" and SubmitInfo_Month=" + item.Value.ToString();;
                        break;
                    case "SubmitInfo_State":
                        wclip += " and isnull(SubmitInfo_State,'0')=" + item.Value.ToString();
                        break;
                    case "OrganizationName":
                        wclip +=" and Organization_Name like '%"+item.Value.Trim().ToString()+"%'";
                        break;
                    default:
                        break;
                }
            }

            var list = ReportService.GetReportStateList(query, wclip, wSubmitinfoclip, reportid);
                return JqGrid(list, query);
          
           
        }

        [HttpPost]
        public ActionResult GetReportPublishList(string id, QueryBase query)
        {
            WhereClip where = T_Report._.Report_IsWeb == "1";
            var list = ReportService.ReprotList(query, where);
            return JqGrid(list, query);
        }

        public ActionResult DeleteSubmitInfo(string ids)
        {
            string Msg = string.Empty;
            Msg = ReportService.DeleteSubmitInfo(ids, CurrUser, out Msg);
            return SNJson(Msg);
        }


        public ActionResult ChooseDate(string ReprotId)
        {

            T_Report Report = ReportService.GetEntity<T_Report>(T_Report._.Report_ID == ReprotId);
            ViewBag.Cycle = Report.Report_Cycle; //报表周期


            //年
            int CurYear = DateTime.Now.Year;
            List<KeyValuePair<string, string>> ListYear = new List<KeyValuePair<string, string>>();
            ListYear.Add(new KeyValuePair<string, string>((CurYear - 1).ToString(), (CurYear - 1).ToString()));
            ListYear.Add(new KeyValuePair<string, string>(CurYear.ToString(), CurYear.ToString()));
            ListYear.Add(new KeyValuePair<string, string>((CurYear + 1).ToString(), (CurYear + 1).ToString()));
            ViewBag.ListYear = ListYear;
            ViewBag.CurYear = CurYear.ToString();
            //月份
            List<KeyValuePair<string, string>> ListMonth = new List<KeyValuePair<string, string>>();
            for (int i = 1; i <= 12; i++)
            {
                ListMonth.Add(new KeyValuePair<string, string>(i.ToString("00"), i.ToString() + "月"));
            }
            ViewBag.CurMonth = DateTime.Now.Month.ToString("00");
            ViewBag.ListMonth = ListMonth;
            //季度
            List<KeyValuePair<string, string>> ListJD = new List<KeyValuePair<string, string>>();
            ListJD.Add(new KeyValuePair<string, string>("01", "第一季度"));
            ListJD.Add(new KeyValuePair<string, string>("02", "第二季度"));
            ListJD.Add(new KeyValuePair<string, string>("03", "第三季度"));
            ListJD.Add(new KeyValuePair<string, string>("04", "第四季度"));
            ViewBag.ListJD = ListJD;
            ViewBag.CurJD = Math.Ceiling((Convert.ToDecimal(DateTime.Now.Month) / 3)).ToString("00");


            //IDictionary<string, DateTime> GetAllDeadline = ReportService.GetAllDeadline();
            if (Report.Report_Cycle == "01") //月报
            {

                    if (DateTime.Now.Month == 1)//如果是元月，往前推一个月，就要变成去年12月
                    {
                        ViewBag.CurMonth = 12.ToString("00");
                        ViewBag.CurYear = DateTime.Now.Year - 1;
                    }
                    else
                    {
                        ViewBag.CurMonth = (DateTime.Now.Month - 1).ToString("00");
                        ViewBag.CurYear = DateTime.Now.Year;
                    }
              


            }
            else if (Report.Report_Cycle == "02")//季报
            {


                    if ((int)Math.Ceiling((Convert.ToDecimal(DateTime.Now.Month) / 3)) == 1)//如果当前为第一季度，则上季度为去年四季度。
                    {
                        ViewBag.CurJD = "04";
                        ViewBag.CurYear = DateTime.Now.Year - 1;
                    }
                    else
                    {
                        ViewBag.CurJD = (Math.Ceiling((Convert.ToDecimal(DateTime.Now.Month) / 3)) - 1).ToString("00");
                        ViewBag.CurYear = DateTime.Now.Year;
                    }
               

            }
            else//年报
            {

                ViewBag.CurYear = DateTime.Now.Year - 1;
            }


            return View();
        }

        public ActionResult ChoosecollectDate(string ReprotId)
        {

            T_Report Report = ReportService.GetEntity<T_Report>(T_Report._.Report_ID == ReprotId);
            ViewBag.Cycle = Report.Report_Cycle; //报表周期
            IDictionary<string, DateTime> GetAllDeadline = ReportService.GetAllDeadline();

            //年
            int CurYear = DateTime.Now.Year;
            List<KeyValuePair<string, string>> ListYear = new List<KeyValuePair<string, string>>();
            ListYear.Add(new KeyValuePair<string, string>((CurYear - 1).ToString(), (CurYear - 1).ToString()));
            ListYear.Add(new KeyValuePair<string, string>(CurYear.ToString(), CurYear.ToString()));
            ListYear.Add(new KeyValuePair<string, string>((CurYear + 1).ToString(), (CurYear + 1).ToString()));
            ViewBag.ListYear = ListYear;
            ViewBag.CurYear = CurYear.ToString();
            //月份
            List<KeyValuePair<string, string>> ListMonth = new List<KeyValuePair<string, string>>();
            for (int i = 1; i <= 12; i++)
            {
                ListMonth.Add(new KeyValuePair<string, string>(i.ToString("00"), i.ToString() + "月"));
            }
            ViewBag.CurMonth = DateTime.Now.Month.ToString("00");
            ViewBag.ListMonth = ListMonth;
            //季度
            List<KeyValuePair<string, string>> ListJD = new List<KeyValuePair<string, string>>();
            ListJD.Add(new KeyValuePair<string, string>("01", "第一季度"));
            ListJD.Add(new KeyValuePair<string, string>("02", "第二季度"));
            ListJD.Add(new KeyValuePair<string, string>("03", "第三季度"));
            ListJD.Add(new KeyValuePair<string, string>("04", "第四季度"));
            ViewBag.ListJD = ListJD;
            ViewBag.CurJD = Math.Ceiling((Convert.ToDecimal(DateTime.Now.Month) / 3)).ToString("00");
            ViewBag.GetOrganizeNodesUrl = Url.Content("~/SystemManage/Organization/GetChildOrganizationNodes/");


            if (Report.Report_Cycle == "01") //月报
            {

                    if (DateTime.Now.Month == 1)//如果是元月，往前推一个月，就要变成去年12月
                    {
                        ViewBag.CurMonth = 12.ToString("00");
                        ViewBag.CurYear = DateTime.Now.Year - 1;
                    }
                    else
                    {
                        ViewBag.CurMonth = (DateTime.Now.Month - 1).ToString("00");
                        ViewBag.CurYear = DateTime.Now.Year;
                    }
              

            }
            else if (Report.Report_Cycle == "02")//季报
            {


                    if ((int)Math.Ceiling((Convert.ToDecimal(DateTime.Now.Month) / 3)) == 1)//如果当前为第一季度，则上季度为去年四季度。
                    {
                        ViewBag.CurJD = "04";
                        ViewBag.CurYear = DateTime.Now.Year - 1;
                    }
                    else
                    {
                        ViewBag.CurJD = (Math.Ceiling((Convert.ToDecimal(DateTime.Now.Month) / 3)) - 1).ToString("00");
                        ViewBag.CurYear = DateTime.Now.Year;
                    }
               

            }
            else//年报
            {

                ViewBag.CurYear =  DateTime.Now.Year - 1 ;
            }

            return View();
        }



        public ActionResult ChooseLazyDate()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SetLazyDate(int submitInfoID, string lazyDate)
        {
            string msg = string.Empty;
            ReportService.SetLazyDate(CurrUser, submitInfoID, DateTime.Parse(lazyDate), out msg);
            return SNJson(msg);
        }

        public ActionResult SetReportPublish(string ReportId, string sYear, string sMonth, string sJd)
        {
            string Msg = string.Empty;
            string OrgId = CurrUser.UserInfo.User_OrganizationID.ToString();
            Msg = ReportService.PublishReport(OrgId, ReportId, sYear, sMonth, sJd,
                CurrUser.UserInfo.User_ID, CurrUser.UserInfo.User_Name);
            return Json(Msg);
        }
    }
}
